package
{
	import Classes.CLoader;
	import Classes.pi_events;
	import Classes.pi_images;
	import Classes.pj_styles;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_imageButton;
	import UIFree.controls.uif_popupMenu;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	import UIFree.uif_styles;
	
	import UIUtils.uif_util;
	
	import api_servers.api_externalserver;
	import api_servers.api_myserver;
	import api_servers.pi_servers;
	import api_servers.pjf_gapiServer;
	import api_servers.pjf_mylocal;
	
	import classes.pj_freeBase;
	import classes.pjf_manager;
	
	import components.pj_albums;
	import components.uif_tabItem;
	import components.uif_tabs;
	import components.uif_tabsLayer;
	
	import controls.pjf_albums;
	import controls.pjf_toolbar;
	
	import dialogs.timeAlerts.pi_alertTime;
	
	import editor.pi_baseToolbar;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.system.Security;
	
	import manager.pi_manager;
	
	public class pj_gapi extends photo_inject {
		private var myADDR: String = 'http://oformi-foto.ru';
		
		public function pj_gapi() {
			photo_inject.APPWIDTH  = stage.stageWidth;
			photo_inject.APPHEIGHT = stage.stageHeight;
			pi_servers.ADDR = [myADDR, myADDR, myADDR, myADDR]; 
			super();
			initialize(externalserver.vars);
		}
		
		override protected function getStyles(): uif_styles {
			return new pj_styles();
		}
		
		override public function get balanceMenu(): Array {
			return [
			];
		} 
		
		override protected function securityInit():void {
			super.securityInit();
			//Security.loadPolicyFile();
			if (externalserver.vars['plugins'])
				pi_servers.PLUGINS_URL = externalserver.vars['plugins'];
			else pi_servers.PLUGINS_URL	= pi_servers.ADDR[3] + '/pj/plugins/default/';
		}
		
		override protected function serversInit(): void {
//			var storageURL: String = 'http://storage-27811-3.cs.clodoserver.ru/';
			var storageURL: String = 'http://vds-14a5.1gb.ru/';
			
			pi_servers.USERPHOTOHOST	= pi_servers.ADDR[1];
			pi_servers.UPLOADJPG_URL	= pi_servers.ADDR[1] + '/games/index_pj.php';
/*			
			pi_servers.MROOT_URL		= pi_servers.ADDR[3] + '/games/data/a/templates/';
			pi_servers.PREVIEW_URL		= 'http://storage-27811-3.cs.clodoserver.ru/jpg_preview/';
*/			
			
			pi_servers.MROOT_URL		= storageURL + 'pj/';
			pi_servers.PREVIEW_URL		= pi_servers.MROOT_URL + 'jpg_preview/';
			
			pi_servers.IMAGESURL		= pi_servers.ADDR[3] + '/pj/images/';
			pi_servers.EFFECTS_URL		= pi_servers.ADDR[3] + '/pj/plugins/filters/';
			pi_servers.FLASH_URL		= getHost() + '/templates'; 			
		}
		
		override protected function startViewCard(params: String): void {
			super.startViewCard(params);
			var inner: Rectangle = topLayer.getInner();
			new uif_imageButton(topLayer, pi_images.c_tofile, inner.x + 24, inner.y, function(e: Event): void {
				saveToJPG(cardbody);
			});
		}
		
		override protected function doReplyAsCard(): void {
//			navigateToURL(new URLRequest(pi_servers.ADDR[2] + '?task=app,view'), '_self');
		}
		
		override protected function extendList(): Object {
			return {
				'tmplLoad'	: {
					id: 0,
					file: 'c_tmplLoad.swf',
					icon: 'c_tmplLoad.swf.png',
					caption: 'Загрузить шаблон',
					load: true
				},
				'SOUND'			: {
					id: 1,
					file: 'c_sound.swf',
					icon: 'c_sound.swf.png',
					caption: 'Добавить музыку к открытке'
				}, 
				'extends'		: {
					id: 2,
					file: 'c_extends_free.swf',
					icon: 'c_extends.swf.png',
					caption: 'Загрузить фотографию',
					load: true
				}, 
				'elems'		: {
					id: 3,
					file: 'c_elems.swf',
					icon: 'c_title.swf.png',
					caption: 'Дополнительные элементы',
					load: true
				},
				'tlt'			: {
					id: 4,
					file: 'c_title.swf'
				},
				'COR'			: {
					id: 5,
					file: 'c_correction.swf',
					load: 5 
				},
				'printer'			: {
					id: 6,
					file: 'c_printer.swf',
					load: 4
				},
				'helperMulti'			: {
					id: 7,
					file: 'c_helperMulti.swf'
				},
				'adminConsol'			: {
					id: 8,
					file: 'c_admin.swf'
				},
				'search'			: {
					id: 9,
					file: 'c_search.swf',
					load: 3
				},
				'FRAMES': {
					id		: 10,
					file	: 'c_frames.swf'
				},
				'VIN': {
					id		: 11,
					file	: 'c_vin.swf'
				}/*,
				'ALBUM': {
					id		: 11,
					file	: 'c_album.swf',
					load	: 1
				}*/
			}
		}		
		
		override public function getClasses(): Object {
			return  uif_util.union(super.getClasses(), {
				alertTime	: pi_alertTime,
				popupMenu	: uif_popupMenu,
				data		: pi_data
			});
		}
		
		override protected function userInitialize(postFunc: Function): void {
			externalserver.userIn(
				function(a_data: Object): void {
					externalserver.viewer_id = a_data.uid;
					postFunc(a_data, null);
				}
			);
		}
		
		override protected function createManager(): pi_manager {
			return new pjf_manager(this);
		}
		
		override protected function createSiteData(): Object {
			var _data: Object = {
				INVITEDESC			: '<b>10 руб. на ваш счет</b> за каждого друга, установившего приложение по вашему приглашению.',
				CURRENCY			: 'руб.',
				FRIENDMAILDESCR		: 'Введите e-mail получателя открытки',//. Используйте запятую для разделения нескольких адресов.\nПример:\nsidorov@mail.ru,petrov@mail.ru,vasechkin@gmail.com';
				CARDLINKDESCRIPTION	: 'Ссылка на открытку: %s',
				ERRORMAILFORMAT		: 'Неверный формат адреса, должно быть такого вида:\nuser_name@yandex.ru',
				SMALLBALANCE		: '<font color="#FF0000"><b>На вашем счету недостаточно средств.</b></font>\nДля текущей операции требуется %s1 руб., ваш текущий баланс %s2 руб.\nНажмите кнопку "ок", если вы хотите пополнить свой счет в приложении.',
				JPGWAIT				: pi_data.JPGWAIT,
				ad: {
					ref_id	: '451859761',
					asMail	: true
				}
			};
			return _data;
		}
		
		override public function initLayers(count: int): void {
			_layers = new Array();
			for (var i: int = 0; i<count; i++)
				_layers.push(new uif_ecomponent(this, 0, 0, APPWIDTH, APPHEIGHT));
		}
		
		override protected function albumsPrepare(uid: String, a_blockAlbums: Array): Array {
			for each (var block: Object in a_blockAlbums) {
				for each (var album: Object in block.albums) {
					album.smallTitle = Utils.ll(album.title?album.title:album.created, 16);
				}
			}
			return a_blockAlbums;
		}
		
		override protected function sd_balance(a_data:*): void {
			if (a_data.balance) dispatchEvent(new ObjectEvent(pi_events.CHANGEBALANCE, a_data.balance));
		}
		
		override protected function createToolbar(): pi_baseToolbar {
			return new pjf_toolbar(_layers[2], 556);
		}
		
		/*		override protected function startViewCard(params: String): void {
		super.startViewCard(params);
		var inner: Rectangle = getInner();
		var createButton: pj_button = new pj_button(topLayer, inner.x, inner.y, 157, 'Создать открытку', function(e: Event): void {
		createButton.dispose();
		fullScreen.dispose();
		initEditor();
		});
		}*/
		
		override protected function afterLoadAlbums(uid: String, a_albums: Array): void {
			var list: Array = albumsPrepare(uid, a_albums);
			if (list.length > 0) {
				/*
				if (!_albums) _albums = new pj_albums(_layers[0], list, 0, 780, APPWIDTH, 100, 'tabBox');
				else _albums.assignAlbums(uid, list);
				*/
				function doChangeAlbumsSource(e: Event): void {
					var tabs: uif_tabs = e.target as uif_tabs;
					if (!_albums) {
						var layer: uif_tabsLayer = (tabs.parent as pjf_albums).layer;
						var rect: Rectangle = layer.getInner();
						_albums = new pj_albums(layer, list[tabs.selectIndex].albums, rect.x, rect.y, rect.width, rect.height, 'tabBox');
					} else _albums.assignAlbums(uid, list[tabs.selectIndex].albums);
				}
				
				new pjf_albums(_layers[0], list, doChangeAlbumsSource, 0, 780, APPWIDTH, height - 780, 0, 'tabBox');
				//_albums
			}
		}
		
		override public function sendCard(): void {
			saveToJPG(cardbody);
		}
		
		override protected function createExternalServer(): api_externalserver {
			return new pjf_gapiServer(pi_servers.ADDR[0] + '/games/index.php', this);
		} 
		
		override protected function createMyServer(): api_myserver {
			return new pjf_mylocal(this);
		}
	}
}